package com.prs.crm.dao.hr.impl;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.hr.LeaveDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.hr.Leave;
import com.prs.crm.domain.hr.LeaveType;
import com.prs.crm.domain.hr.Person;

public class LeaveDaoImpl extends BaseDaoImpl<Leave> implements LeaveDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<LeaveType> getAllLeaveType() {
		Criteria criteria = this.getSession().createCriteria(LeaveType.class);
		criteria.addOrder(Order.desc("recordTime"));
		return criteria.list();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Object[]> findByType(final LeaveType type,final Date startTime,final Date endTime,final Collection<Person> persons)
	{
		return (Collection<Object[]> )this.getHibernateTemplate().execute(
			      new HibernateCallback() {
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(Leave.class);
				        	if (type!=null)
				        	{
				        		criteria.add(Restrictions.eq("type", type));
				        	}
				        	if (startTime!=null)
				        	{
				        		criteria.add(Restrictions.ge("beginTime", startTime));
				        	}
				        	if (endTime!=null)
				        	{
				        		criteria.add(Restrictions.le("beginTime", endTime));
				        	}
				        	if (persons!=null)
				        	{
				        		criteria.add(Restrictions.in("recorder", persons));
				        	}
				        	criteria.add(Restrictions.eq("state", Constrains.TASK_PASS));
				        	ProjectionList projectionlist=Projections.projectionList();  
				        	projectionlist.add(Projections.groupProperty("recorder"));
				        	projectionlist.add(Projections.rowCount());
				        	criteria.setProjection(projectionlist);
				        	/*
				        	Collection result = criteria.list();
				        	for (Object[] o : (Collection<Object[]>)result)
				        	{
				        		System.out.println("----------------------------------");
				        		for (Object value : o)
				        		{
				        			System.out.println(value.toString()+"  <--");
				        		}
				        	}*/
				        	/*
				        	criteria.setProjection(Projections.rowCount());
				        	int count = (Integer)criteria.uniqueResult();
				        	criteria.setProjection(null);
				        	*/
				        	return criteria.list();
				        }   
				 } );
	}
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Leave> findByPerson(final Person person) {
		return (Collection<Leave>)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Leave.class);
				if(person != null){
					criteria.add(Restrictions.eq("recorder", person));
				}
				criteria.add(Restrictions.eq("state","3"));
				return criteria.list();
			}
			
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Leave> getAllLeave() {
		Criteria criteria = this.getSession().createCriteria(Leave.class);		
		return criteria.list();
	}
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Leave> getLeaveByIds(Collection<Integer> ids) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Leave.class);
		criteria.add(Restrictions.in("id", ids));
		return criteria.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Leave> getLeavesByPersonInMonth(final Person person, final Integer year,
			final Integer month) {
		// TODO Auto-generated method stub
		return (Collection<Leave>)this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				System.out.println(month);
				Calendar min = Calendar.getInstance();
				min.set(Calendar.YEAR, year);
				min.set(Calendar.MONTH, month - 1);
				min.set(Calendar.DAY_OF_MONTH, 1);	
				
				
				Calendar max = Calendar.getInstance();
				max.set(Calendar.YEAR, year);
				max.set(Calendar.MONTH, month);	
				max.set(Calendar.DAY_OF_MONTH, 1);
				
				Criteria criteria = arg0.createCriteria(Leave.class);
				criteria.add(Restrictions.ge("beginTime", min.getTime()));
				criteria.add(Restrictions.lt("beginTime", max.getTime()));
				criteria.add(Restrictions.eq("recorder", person));
				criteria.add(Restrictions.eq("state", Constrains.TASK_PASS));
				return criteria.list();
			}
			
		});
	}

}
